package shop.core.task;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import shop.services.manage.offline.OfflineService;
import shop.services.manage.offline.bean.Offline;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;

/**
 * Created by wzl on 2017/7/25.
 */
@Component
public class UpdateOfflineStatusTask implements Runnable {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(UpdateOfflineStatusTask.class);
    @Autowired
    private OfflineService offlineService;

    public void setOfflineService(OfflineService offlineService) {
        this.offlineService = offlineService;
    }

    @Override
    public void run() {
        Offline offline = new Offline();
        List<Offline> offlineList = offlineService.selectList(offline);
        if (offlineList != null) {
            logger.error("offlineList=" + offlineList.size());
            for (int i = 0; i < offlineList.size(); i++) {
                Offline offlineInfo = offlineList.get(i);
                String status = offlineInfo.getStatus();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date();
                String date2 = sdf.format(new Date());
                String date3 = sdf.format(new Date(date.getTime() + 3 * 24 * 60 * 60 * 1000));
                if ("1".equals(status)) {
                    if (date3.compareTo(offlineInfo.getBeginTime()) > 0) {
                        offlineInfo.setStatus("5");
                        offlineService.update(offlineInfo);
                    } else {
                    }

                } else if ("2".equals(status)) {
                    if (date2.compareTo(offlineInfo.getBeginTime()) >= 0 && date2.compareTo(offlineInfo.getEndTime()) <= 0) {
                        offlineInfo.setStatus("3");
                        offlineService.update(offlineInfo);
                    } else if (date2.compareTo(offlineInfo.getEndTime()) > 0) {
                        offlineInfo.setStatus("4");
                        offlineService.update(offlineInfo);
                    } else {
                    }

                } else if ("3".equals(status)) {
                    if (date2.compareTo(offlineInfo.getEndTime()) > 0) {
                        offlineInfo.setStatus("4");
                        offlineService.update(offlineInfo);
                    } else {
                    }
                }
            }
        }
    }
}
